<?php
namespace Study\fastroute;
require_once '../../vendor/autoload.php';


$dispatcher = \FastRoute\simpleDispatcher(function(\FastRoute\RouteCollector $r) {
    $r->get('/test', function(){
        var_dump('user');
    });


    $r->addGroup('/user', function() use ($r){
        $r->get('/list',function (){var_dump('list');});
        $r->get('/detail',function (){var_dump('detail');});
    });

    /***匹配到控制器**************************************************************************************************/
    //
    $r->addGroup('/article', function() use ($r){
        $r->get('/list',[Article::class,'lists']);
        $r->get('/detail/{id}','Article.detail');
    });

});


// Fetch method and URI from somewhere
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];

// Strip query string (?foo=bar) and decode URI
if (false !== $pos = strpos($uri, '?')) {
    $uri = substr($uri, 0, $pos);
}

var_dump($uri);
$uri = rawurldecode($uri);
$routeInfo = $dispatcher->dispatch($httpMethod, $uri);
switch ($routeInfo[0]) {
    case \FastRoute\Dispatcher::NOT_FOUND:
        // ... 404 Not Found
        var_dump('404 Not Found');
        break;
    case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
        $allowedMethods = $routeInfo[1];
        // ... 405 Method Not Allowed
        var_dump('405 Method Not Allowed');
        break;
    case \FastRoute\Dispatcher::FOUND:
        $handler = $routeInfo[1];
        $vars = $routeInfo[2];

        /***匹配控制器和方法******************************************/
        $controller = new $handler[0](); // 实例化控制器
        call_user_func_array([$controller, $handler[1]], $vars); // 调用控制器方法
        // ... call $handler with $vars
        break;
}


namespace Study\fastroute;
class Article
{
    public function lists()
    {

    }

    public function detail()
    {

    }
}